/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package VISUAL;
import DAL.ConectaBd;
import java.sql.*;
import javax.swing.JOptionPane;
import net.proteanit.sql.DbUtils;

import java.io.InputStream;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import net.sf.jasperreports.engine.JRException;
import postgre.ConnectionFactory;
import utils.ReportUtils;


/**
 *
 * @author MAYCON
 */
public class frmConsultaCliente extends javax.swing.JInternalFrame {
    Connection conecta;
    PreparedStatement pst;
    ResultSet rs;

    /**
     * Creates new form frmConsultaCliente
     */
    public frmConsultaCliente() throws ClassNotFoundException {
        initComponents();
        this.setLocation(450,100);
        conecta = ConectaBd.conectabd();
        ListarClientes();
    }
    
    public void ListarClientes()//metodo para aparecer os nomes cadastrados no banco na tabela
    {
        String sql = "Select *from cadcliente order by codigo Asc";// order by codigo Asc para deixar em ordem por código crescente
        try
        {
            pst = conecta.prepareStatement(sql);
            rs = pst.executeQuery();
            tblClientesCadastrados.setModel(DbUtils.resultSetToTableModel(rs));
            
        }
        catch(SQLException error)
        {
            JOptionPane.showMessageDialog(null,error);
        }    
    }
    
    public void PesquisaClienteCpf()//metodo para funcionar o campo pesquisar, ao escrever o nome do usuário
    {
        String sql = "Select *from cadcliente where cpf like ?";
        
        try
        {
            pst = conecta.prepareStatement(sql);
            pst.setString(1,txtConsultaCpfCliente.getText()+"%");
            rs = pst.executeQuery();
            tblClientesCadastrados.setModel(DbUtils.resultSetToTableModel(rs));
            
        }
        catch(SQLException error)
        {
            JOptionPane.showMessageDialog(null,error);
        }    
    }
    
    public void PesquisaClienteNome()//metodo para funcionar o campo pesquisar, ao escrever o nome do usuário
    {
        String sql = "Select *from cadcliente where nome like ?";
        
        try
        {
            pst = conecta.prepareStatement(sql);
            pst.setString(1,txtNomeCliente.getText()+"%");
            rs = pst.executeQuery();
            tblClientesCadastrados.setModel(DbUtils.resultSetToTableModel(rs));
            
        }
        catch(SQLException error)
        {
            JOptionPane.showMessageDialog(null,error);
        }    
    }
    
   // public void abrirRelatorioClientes(){
        
        //InputStream inputStream = getClass().getResourceAsStream("/Cliente.jasper");
        
        //Map parametros = new HashMap();
        
        //try{
            
            //ReportUtils.openReport ( "Cliente", inputStream, parametros, 
                   // ConnectionFactory.getEngenheiroConnection());
       // } catch (SQLException exc){
         //   exc.printStackTrace();
       // } catch ( JRException exc ){
        //    exc.printStackTrace();
       // }
   // }
    
    
    
    
    
   
    
    
    
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblClientesCadastrados = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        txtConsultaCpfCliente = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jButton4 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        txtNomeCliente = new javax.swing.JTextField();

        setClosable(true);
        setIconifiable(true);
        setTitle("Consulta de Clientes");
        setName(""); // NOI18N
        setPreferredSize(new java.awt.Dimension(632, 442));

        tblClientesCadastrados.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(tblClientesCadastrados);

        jLabel1.setText("Nome");

        txtConsultaCpfCliente.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtConsultaCpfClienteKeyReleased(evt);
            }
        });

        jLabel2.setText("CPF");

        jLabel11.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ICONES/zoom.png"))); // NOI18N
        jLabel11.setText("PESQUISAR");

        jLabel12.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ICONES/zoom.png"))); // NOI18N
        jLabel12.setText("PESQUISAR");

        jButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ICONES/printer.png"))); // NOI18N
        jButton4.setText("Relatório");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jLabel3.setText("Imprimir Relatório:");

        txtNomeCliente.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtNomeClienteKeyReleased(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 704, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton4))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtNomeCliente))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtConsultaCpfCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 206, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel12)
                            .addComponent(jLabel11))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {txtConsultaCpfCliente, txtNomeCliente});

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel1, jLabel2});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtNomeCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11))
                .addGap(9, 9, 9)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtConsultaCpfCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12))
                .addGap(73, 73, 73)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton4)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 312, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {txtConsultaCpfCliente, txtNomeCliente});

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jLabel1, jLabel2});

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        
    }//GEN-LAST:event_jButton4ActionPerformed

    private void txtConsultaCpfClienteKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtConsultaCpfClienteKeyReleased
        // TODO add your handling code here:
        PesquisaClienteCpf();
    }//GEN-LAST:event_txtConsultaCpfClienteKeyReleased

    private void txtNomeClienteKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtNomeClienteKeyReleased
        // TODO add your handling code here:
        PesquisaClienteNome();
    }//GEN-LAST:event_txtNomeClienteKeyReleased


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblClientesCadastrados;
    private javax.swing.JTextField txtConsultaCpfCliente;
    private javax.swing.JTextField txtNomeCliente;
    // End of variables declaration//GEN-END:variables

    private ResultSet conecta(ResultSet rs) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}
